<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small"
             :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="设备类型" clearable>
          <el-option
            v-for="dict in dict.type.xw_device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="SN码" prop="deviceSn">
        <el-input
          v-model="queryParams.deviceSn"
          placeholder="请输入设备SN码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理绑定" prop="bingFlag">
        <el-select v-model="queryParams.bingFlag" placeholder="请选择是否绑定代理商" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否需要补货" prop="repairFlag">
        <el-select v-model="queryParams.repairFlag" placeholder="请选择是否需要补货" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['washer:devicemanage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['washer:devicemanage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['washer:devicemanage:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['washer:devicemanage:export']"
        >导出</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="showDownload"
        >下载</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="devicemanageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xw_device_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="设备SN码" align="center" prop="deviceSn" />
      <el-table-column label="批量下载标记" align="center" prop="downloadBatch" />
      <el-table-column label="代理商openid" align="center" prop="agentOpenId" />
      <el-table-column label="终端openid" align="center" prop="enduserOpenId" />
      <el-table-column label="部署地址" align="center" prop="deviceAddress" />
      <el-table-column label="绑定状态" align="center" prop="bingFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.bingFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="是否需要补货" align="center" prop="repairFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.repairFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['washer:devicemanage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['washer:devicemanage:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="downloadQRcode(scope.row,1)"
          >体验版二维码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="downloadQRcode(scope.row,2)"
          >生产二维码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.xw_device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备SN码" prop="deviceSn">
          <el-input :disabled="true" v-model="form.deviceSn" placeholder="自动生成" />
        </el-form-item>

        <el-form-item label="格子数量" prop="gridNumber">
          <el-input-number v-model="form.gridNumber" :min="1" :max="12" label="描述文字"></el-input-number>
        </el-form-item>

        <el-form-item v-if="form.deviceId==null" label="生成设备的数量" prop="deviceCount">
          <el-input-number v-model="form.deviceCount" :min="1" :max="20" label=""></el-input-number>
        </el-form-item>
        <el-form-item v-if="form.deviceId==null" label="批量下载标记" prop="downloadBatch">
          <el-input  v-model="form.downloadBatch" placeholder="" />
        </el-form-item>

        <el-form-item label="1" prop="deviceCount">

        </el-form-item>


        <el-form-item label="1、格子类型和产品" prop="grid01ProductId">
          <el-cascader @change="handleChange"
            v-model="form.grid01ProductId"clearable
            :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="1、状态" prop="grid01StateType">
          <el-select  v-model="form.grid01StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="2、格子类型和产品" prop="grid02ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid02ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="2、状态" prop="grid02StateType">
          <el-select  v-model="form.grid02StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="3、格子类型和产品" prop="grid03ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid03ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="3、状态" prop="grid03StateType">
          <el-select  v-model="form.grid03StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="4、格子类型和产品" prop="grid04ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid04ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="4、状态" prop="grid04StateType">
          <el-select  v-model="form.grid04StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="5、格子类型和产品" prop="grid05ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid05ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="5、状态" prop="grid05StateType">
          <el-select  v-model="form.grid05StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="6、格子类型和产品" prop="grid06ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid06ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="6、状态" prop="grid06StateType">
          <el-select  v-model="form.grid06StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="7、格子类型和产品" prop="grid07ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid07ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="7、状态" prop="grid07StateType">
          <el-select  v-model="form.grid07StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="8、格子类型和产品" prop="grid08ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid08ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="8、状态" prop="grid08StateType">
          <el-select  v-model="form.grid08StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="9、格子类型和产品" prop="grid09ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid09ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="9、状态" prop="grid09StateType">
          <el-select  v-model="form.grid09StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="10、格子类型和产品" prop="grid10ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid10ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="10、状态" prop="grid10StateType">
          <el-select  v-model="form.grid10StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="11、格子类型和产品" prop="grid11ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid11ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="11、状态" prop="grid11StateType">
          <el-select  v-model="form.grid11StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="12、格子类型和产品" prop="grid12ProductId">
          <el-cascader @change="handleChange"
                       v-model="form.grid12ProductId"clearable
                       :options="gridOptions"></el-cascader>
        </el-form-item>

        <el-form-item label="12、状态" prop="grid12StateType">
          <el-select  v-model="form.grid12StateType" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.xw_service_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :inline="true" :model="form2" :rules="rules2" label-width="100px">
        <el-form-item label="批量下载标识" prop="downloadBatch">
          <el-select v-model="form2.downloadBatch" filterable placeholder="选择批量标识">
            <el-option
              v-for="dict in downloadBatchList"
              :key="dict.downloadBatch"
              :label="dict.downloadBatch"
              :value="dict.downloadBatch"

            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevicemanage, getDevicemanage, delDevicemanage, addDevicemanage, updateDevicemanage ,getAllDownloadBatch} from "@/api/washer/devicemanage";
import { listProduct } from "@/api/washer/product";
export default {
  name: "Devicemanage",
  dicts: ['sys_yes_no', 'xw_device_grid','xw_device_type','xw_state_type','xw_service_state'],
  data() {
    return {
      title2:"",
      open2:false,
      form2:{},
      rules2: {},
      downloadBatchList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      devicemanageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      gridOptions:[],
      productList:[],
      dictProduct:[],
      mylist:[{col01:'grid01ProductId',col02:'grid01StateType'},
              {col01:'grid02ProductId',col02:'grid02StateType'}
              ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceType: null,
        deviceSn: null,
        agentOpenId: null,
        washballPrimePrice: null,
        washballSpecialPrice: null,
        terminalOpenId: null,
        bingFlag: null,
        salesGrid01: null,
        rentGrid01: null,
        stopGrid01: null,
        repairFlag:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceType: [
          { required: true, message: "设备类型;1、4G版 2、蓝牙版不能为空", trigger: "change" }
        ],
        washballPrimePrice: [
          { required: true, message: "洗衣球购买原价不能为空", trigger: "blur" }
        ],
        washballSpecialPrice: [
          { required: true, message: "洗衣球购买优惠价不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getProductList();
    this.getList();
  },
  methods: {
    showDownload(){
      getAllDownloadBatch({}).then(res=>{
        console.log(res);
        var downloadBatchList=res.data;
        this.downloadBatchList=downloadBatchList;
        this.open2=true;
      })
    },
    getProductList(){
      listProduct({}).then(res=>{

          var productList=res.rows;
          var dict1=[];
          var dict2=[];
          for(var i=0;i<productList.length;i++){
            if(productList[i].productType=="1"){
              dict1.push({label:productList[i].productName,value:''+productList[i].productId});
            }else{
              dict2.push({label:productList[i].productName,value:''+productList[i].productId});
            }
            this.productList.push({productId:''+productList[i].productId,productName:productList[i].productName});
          }
          console.log(this.productList);
        this.gridOptions=[{
          value:'1',
          label:'出售',
          children:dict1
        },{
          value:'2',
          label:'出租',
          children:dict2}];
      });

    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listDevicemanage(this.queryParams).then(response => {
        this.devicemanageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel2() {
      this.open2 = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        deviceType: null,
        deviceSn: null,
        agentOpenId: null,
        washballPrimePrice: null,
        washballSpecialPrice: null,
        terminalOpenId: null,
        bingFlag: null,
        salesGrid01: null,
        rentGrid01: null,
        stopGrid01: null,
        createBy: null,
        createdTime: null,
        updateBy: null,
        updatedTime: null,
        grid01StateType:null,
        grid01ProductId:null,

        grid02StateType:null,
        grid02ProductId:null,

        grid03StateType:null,
        grid03ProductId:null,

        grid04StateType:null,
        grid04ProductId:null,

        grid05StateType:null,
        grid05ProductId:null,

        grid06StateType:null,
        grid06ProductId:null,

        grid07StateType:null,
        grid07ProductId:null,

        grid08StateType:null,
        grid08ProductId:null,

        grid09StateType:null,
        grid09ProductId:null,

        grid10StateType:null,
        grid10ProductId:null,

        grid11StateType:null,
        grid11ProductId:null,

        grid12StateType:null,
        grid12ProductId:null,
        deviceCount:null,
        gridNumber:null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids
      getDevicemanage(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
        this.decode();
      });
    },
    submitForm2() {
      var downloadBatch=this.form2.downloadBatch;
      if(downloadBatch==null||downloadBatch==""){
        alert("请选择批号标识");
      }
      const baseURL = process.env.VUE_APP_BASE_API
      this.$download.zip("/washer/devicemanage/downloadBatch/zipfile?downloadBatch=" +
        downloadBatch, "设备信息下载.zip");
      this.open2=false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.encode();
          if (this.form.deviceId != null) {
            updateDevicemanage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevicemanage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    encode(){
      var newform=this.form;
      for(var i=1;i<=12;i++){
        var name="";
        if(i<10){
          name="grid0"+i+"ProductId";
        }else{
          name="grid"+i+"ProductId";
        }
        var obj=newform[name];
        if(obj!=null&&obj.length>0){
          newform[name]=obj[0]+","+obj[1];
        }else{
          newform[name]="";
        }
      }
      this.form=newform;
    },
    decode(){
      var newform=this.form;
      for(var i=1;i<=12;i++){
        var name="";
        if(i<10){
          name="grid0"+i+"ProductId";
        }else{
          name="grid"+i+"ProductId";
        }
        var obj=newform[name];
        if(obj!=null&&obj!=""){
          newform[name]=obj.split(",");
        }else{
          newform[name]="";
        }
      }
      this.form=newform;
    },
    handleChange(value){
      console.log(value);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$modal.confirm('是否确认删除设备管理编号为"' + deviceIds + '"的数据项？').then(function() {
        return delDevicemanage(deviceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('washer/devicemanage/export', {
        ...this.queryParams
      }, `devicemanage_${new Date().getTime()}.xlsx`)
    },
    downloadQRcode(row,type){
      var deviceSn=row.deviceSn;
      var fileName="";
      if(type==1){
        fileName=deviceSn+"_trial.jpg"
      }else{
        fileName=deviceSn+"_release.jpg"
      }
      this.$download.zip("/washer/devicemanage/download/QRcode/"+deviceSn+"/"+type, fileName);
    }
  }
};
</script>
